--
-- 
--
function TitanCattleProdButton_OnLoad()
	TitanCattleProd_SetStringsLocale();
	
	this.registry = {
		id = TITAN_CATTLE_PROD_ID,
		menuText = "CattleProd",
		buttonTextFunction = "TitanCattleProdButton_GetButtonText",
		tooltipTitle = "CattleProd",
		tooltipTextFunction = "TitanCattleProdButton_GetTooltipText",
		icon = "Interface\\Icons\\INV_Wand_02",
		iconWidth = 16,
		savedVariables = {
			ShowIcon = 1,
		},
				
		version = TITAN_CATTLE_PROD_VERSION,
		category = "Interface"
	};	
end

--
--
--
function TitanCattleProdButton_OnEvent()
	TitanPanelButton_UpdateButton(TITAN_CATTLE_PROD_ID);
end

--
--
--
function TitanCattleProdButton_OnClick(button)
	if (button == "LeftButton") then
		CattleProd_Configure();
	end
end

--
-- CattleProd button text.
--
function TitanCattleProdButton_GetButtonText(id)
	if (CattleProd_InBattleground())
	then
		-- in battelground
		if (CattleProd_MonitorStarted())
		then
			return TitanCattleProd_LocaleString(TitanCattleProd_MENU_TEXT_MONITORING);
		else
			return TitanCattleProd_LocaleString(TitanCattleProd_MENU_TEXT_STOPPED);
		end
	else
		-- in world
		local autoStartMonitor = CattleProd_GetSetting(CattleProd_AUTO_START_MONITOR);
		if (autoStartMonitor)
		then
			return TitanCattleProd_LocaleString(TitanCattleProd_MENU_TEXT_AUTOMATIC);
		else
			return TitanCattleProd_LocaleString(TitanCattleProd_MENU_TEXT_MANUAL);
		end
	end
end

--
-- CattleProd tooltip.
--
function TitanCattleProdButton_GetTooltipText()
	local tooltip = TitanCattleProd_LocaleString(TitanCattleProd_SETTINGS_HEADER);

	-- report players AFK
	local reportPlayersAFK = CattleProd_GetSetting(CattleProd_REPORT_PLAYERS_AFK);
	if (reportPlayersAFK)
	then
		reportPlayersAFK = TitanCattleProd_LocaleString(TitanCattleProd_SETTING_TRUE);
	else
		reportPlayersAFK = TitanCattleProd_LocaleString(TitanCattleProd_SETTING_FALSE);
	end
	tooltip = string.format(TitanCattleProd_LocaleString(TitanCattleProd_SETTINGS_REPORT_PLAYERS_AFK),
		tooltip, reportPlayersAFK);

	-- required effort
	local requiredEffort = CattleProd_GetSetting(CattleProd_REQUIRED_EFFORT_PERCENTAGE);
	tooltip = string.format(TitanCattleProd_LocaleString(TitanCattleProd_SETTINGS_REQUIRED_EFFORT),
		tooltip, requiredEffort * 100);

	-- report known leeches
	local reportKnownLeeches = CattleProd_GetSetting(CattleProd_REPORT_KNOWN_LEECHES);
	if (reportKnownLeeches)
	then
		reportKnownLeeches = TitanCattleProd_LocaleString(TitanCattleProd_SETTING_TRUE);
	else
		reportKnownLeeches = TitanCattleProd_LocaleString(TitanCattleProd_SETTING_FALSE);
	end
	tooltip = string.format(TitanCattleProd_LocaleString(TitanCattleProd_SETTINGS_REPORT_KNOWN_LEECHES),
		tooltip, reportKnownLeeches);

	return tooltip;
end

--
-- Create right mouse click menu.
--
function TitanPanelRightClickMenu_PrepareCattleProdMenu()
	TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_CATTLE_PROD_ID].menuText);

	local inBattleground = CattleProd_InBattleground();
	local option;

	-- configure
	option = {};
	option.text = TitanCattleProd_Colored(TITAN_CATTLE_PROD_COLOR_ENABLED,
		TitanCattleProd_LocaleString(TitanCattleProd_OPTION_CONFIGURE));
	option.func = CattleProd_Configure;
	UIDropDownMenu_AddButton(option);

	-- herd
	option = {};
	if (inBattleground)
	then
		option.text = TitanCattleProd_Colored(TITAN_CATTLE_PROD_COLOR_ENABLED,
			TitanCattleProd_LocaleString(TitanCattleProd_OPTION_REPORT_LEECHES));
		option.func = CattleProd_Main;
	else
		option.text = TitanCattleProd_Colored(TITAN_CATTLE_PROD_COLOR_DISABLED,
			TitanCattleProd_LocaleString(TitanCattleProd_OPTION_REPORT_LEECHES));
		option.func = TitanCattleProd_DisabledFunction;
	end	
	UIDropDownMenu_AddButton(option);

	-- defend
	option = {};
	if (inBattleground)
	then
		option.text = TitanCattleProd_Colored(TITAN_CATTLE_PROD_COLOR_ENABLED,
			TitanCattleProd_LocaleString(TitanCattleProd_OPTION_CHECK_DEFENDING));
		option.func = CattleProd_Defend;
	else
		option.text = TitanCattleProd_Colored(TITAN_CATTLE_PROD_COLOR_DISABLED,
			TitanCattleProd_LocaleString(TitanCattleProd_OPTION_CHECK_DEFENDING));
		option.func = TitanCattleProd_DisabledFunction;
	end	
	UIDropDownMenu_AddButton(option);
end

--
-- Get colored text.
--
-- color Color
-- text Text
-- return Colored Text
--
function TitanCattleProd_Colored(color, text)
	CattleProd_AssertType(color, "string", "Illegal Argument");
	CattleProd_AssertType(text, "string", "Illegal Argument");
	return color .. text .. "|r";
end

--
-- Disabled function.
--
function TitanCattleProd_DisabledFunction()
	-- do nothing
end
